//
//  MainViewController.swift
//  微博天下

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //在这里实现把所有的控制器添加到主流框架
       
        setChildController()
       
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        customBtn()
    }
    
    
    //写一个方法去实现4个控制器的添加
    private func setChildController() {
         addChildViewController(HomeTableViewController(), imageNormal: "tabbar_home_highlighted", imageSeleted: "tabbar_home_highlighted", title: "主页")
        
          addChildViewController(MessageTableViewController(), imageNormal: "tabbar_message_center", imageSeleted: "tabbar_message_center_highlighted", title: "消息")
        
        //1、在 中间直接加入一个controller
        addChildViewController(UIViewController())
        
         addChildViewController(DiscoverTableViewController(), imageNormal: "tabbar_discover", imageSeleted: "tabbar_discover_highlighted", title: "发现")
         addChildViewController(MyInfoTableViewController(), imageNormal: "tabbar_profile", imageSeleted: "tabbar_profile_highlighted", title: "我")
    }
    
    /**
    *  定义一个函数去实现不同控制器的图片和标题的添加
    */
    private func addChildViewController(childController: UIViewController ,imageNormal: String ,imageSeleted: String , title: String){
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageNormal)
        
        childController.tabBarItem.selectedImage = UIImage(named: imageSeleted)
        
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
    }
    
    //创建一个button放在相应的位置上
    private func customBtn(){
 /// 平均宽度  ，总长度 / 个数
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count)
        let rect = CGRect(x: 0, y: 0 , width: w, height: tabBar.bounds.height)
        centerButton.frame  = CGRectOffset(rect, 2 * w, 0)
        
    }
    //mark --懒加载  ,本质就是闭包
    private lazy var centerButton:UIButton = {
       let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //懒加载要使用self
        
        self.tabBar.addSubview(btn)
        btn.addTarget(self, action: "clickEditMricoblog", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
     func clickEditMricoblog(){
        
        //判断 是否登录 而知道跳转界面
        let vc = TSZUserAcountModel.userLogin ? TSZEditMricoblog() : TSZOAuthViewController()
       let navVc = UINavigationController(rootViewController: vc)
      presentViewController(navVc, animated: true, completion: nil)
    }
}
